package com.etoak.config;

import cn.hutool.core.util.StrUtil;
import com.etoak.common.core.properties.ImageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

@Configuration
public class WebFluxConfig implements WebFluxConfigurer {
/**
 * 路由实现 静态资源访问
 *
 * 因为当前项目只能读到配置中心 gateway.yml 和 本地的配置文件 无法读取到Nacos配置中心的 upload-service-dev.yml文件
 * 所以使用Nacos 共享文件
 *
 * 访问路径 http://localhost:8080/pics/3b7288c8b9e9488c939f774ce448c0a2.jpg
 */


    @Autowired

    ImageProperties imageProperties;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // mapping = /pics/**
        String mapping = imageProperties.getMapping().endsWith(StrUtil.SLASH) ?
                imageProperties.getMapping() + "**" :
                imageProperties.getMapping() + "/**";
        // location = file:d:/et2407/

        String location = imageProperties.getLocation().endsWith(StrUtil.SLASH) ?
                "file:" + imageProperties.getLocation() :
                "file:" + imageProperties.getLocation() + StrUtil.SLASH;

        registry.addResourceHandler(mapping).addResourceLocations(location);
    }

}
